package com.flute.tools.sql;

import java.io.Serializable;

import com.flute.tools.data.DataRecord;

public class InsertSQL implements ISQL,Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7951460546071669844L;
	private boolean ignore;
	private boolean override;
	private SQLTable tables;
	private FieldValuesSQL fieldValues;

	public InsertSQL() {
		this.tables = new SQLTable();
		this.fieldValues = new FieldValuesSQL();
		this.ignore = true;
	}

	public void addTable(String table) {
		this.tables.addTable(table);
	}

	public void addFieldValue(String field, String value) {
		this.fieldValues.addFieldValue(field, value);
	}

	public void addFieldValue(DataRecord dataRecord) {
		for (String field : dataRecord.getAllFields()) {
			this.fieldValues.addFieldValue(field, dataRecord
					.getFieldValue(field));
		}
	}

	public boolean isIgnore() {
		return ignore;
	}

	public void setIgnore(boolean ignore) {
		this.ignore = ignore;
	}

	public boolean isOverride() {
		return override;
	}

	public void setOverride(boolean override) {
		this.override = override;
	}

	@Override
	public String getSQLString() {

		StringBuffer sb = new StringBuffer();
		sb.append("insert into ");
		if (ignore) {
			sb.append(" ignore ");
		}
		sb.append(tables.getSQLString());
		sb.append(" set ");
		sb.append(fieldValues.getSQLString());
		if (override) {
			sb.append(" on duplicate key update ");
			sb.append(fieldValues.getSQLString());
		}

		return SQLUtils.removeMultiBlank(sb.toString().trim());
	}

}
